package com.vincent.baseapp.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.vincent.baseapp.util.CommonUtil;
import com.vincent.baseapp.util.VLog;
import com.vincent.baseapp.util.VToast;

import butterknife.ButterKnife;


public abstract class BaseFragment<V, T extends BasePresenter<V>> extends Fragment implements IBaseView {
    public T presenter;
    public Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        presenter = initPresenter();
        presenter.attach((V) this);
        mContext = context;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(getLayoutResId(), container, false);
        ButterKnife.bind(this, view);
        initViewData();
        return view;
    }


    /**
     * get contentView resourceId
     *
     * @return
     */
    protected abstract int getLayoutResId();


    /**
     * init component data
     */
    protected abstract void initViewData();

    public abstract T initPresenter();


    @Override
    public void showLoadDialog() {
        CommonUtil.showLoadingView(getActivity());
    }

    @Override
    public void dismissLoadDialog() {
        CommonUtil.dismissLoadingView(getActivity());
    }

    @Override
    public void showToast(String content) {
        VToast.showLong(content);
    }

    @Override
    public void onDetach() {
        presenter.detach();
        super.onDetach();
    }

    @Override
    public void onDestroyView() {
        ButterKnife.unbind(this);
        super.onDestroyView();
    }

    public void Lg(String message) {
        VLog.d(getClass().getSimpleName(), message);
    }

    public void onResume() {
        super.onResume();
//        MobclickAgent.onPageStart(this.getClass().getSimpleName());
    }

    public void onPause() {
        super.onPause();
//        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
    }
}
